package com.mindbook.vip.gawxchatinfo.controller.user;

import com.mindbook.vip.gawxchatinfo.entity.user.UserFront;
import com.mindbook.vip.gawxchatinfo.entity.user.UserFrontInfo;
import com.mindbook.vip.gawxchatinfo.service.user.UserFrontInfoService;
import com.mindbook.vip.gawxchatinfo.utils.crud.CrudUtils;
import com.mindbook.vip.gawxchatinfo.utils.result.Result;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultEnum;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户基础信息
 */
@RestController
@RequestMapping("/front/user/info")
@Api(tags = "前端用户关联信息")
public class UserFrontInfoController {

    @Autowired private UserFrontInfoService userFrontInfoService;

    /**
     * 数据保存
     * @param userFrontInfo
     * @return
     */
    @PostMapping("/")
    @ApiOperation(value = "新增")
    public Result add(UserFrontInfo userFrontInfo){
        //获取到当前用户的id,并将用户的id作为用户信息的id进行保存
        UserFront userFront = (UserFront) SecurityUtils.getSubject().getPrincipal();

        userFrontInfo.setId(userFront.getId());

        //数据持久化
        Integer add = userFrontInfoService.add(userFrontInfo);

        CrudUtils.validateAddResult(add);

        return ResultUtils.genSuccessResult(ResultEnum.DATA_ADD_SUCCESS);
    }

    /**
     * 修改
     * @param userFrontInfo
     * @return
     */
    @PutMapping("/")
    @ApiOperation(value = "修改")
    public Result update(UserFrontInfo userFrontInfo){
        //获取到当前用户的id,并将用户的id作为用户信息的id进行保存
        UserFront userFront = (UserFront) SecurityUtils.getSubject().getPrincipal();

        userFrontInfo.setId(userFront.getId());

        Integer put = userFrontInfoService.put(userFrontInfo);

        CrudUtils.validateUpdateResult(put);

        return ResultUtils.genSuccessResult(ResultEnum.DATA_UPDATE_SUCCESS);
    }


    @GetMapping("/select")
    @ApiOperation(value="查询当前用户的个人信息")
    public Result findBySecurity(){
        //获取前段用户个人信息的编号
        UserFront userFront = (UserFront) SecurityUtils.getSubject().getPrincipal();

        Integer id = userFront.getId();

        //查询个人信息
        UserFrontInfo byPrimaryKey = userFrontInfoService.findByPrimaryKey(id);

        return ResultUtils.genSuccessResult(byPrimaryKey);
    }



}
